package com.snow.fvmusic.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.snow.fvmusic.bo.WebConst;
import com.snow.fvmusic.dao.ArticleVODao;
import com.snow.fvmusic.generator.entities.MusicPool;
import com.snow.fvmusic.generator.mapper.ArticleMapper;
import com.snow.fvmusic.generator.mapper.MusicPoolMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @Author ：snow
 * @Date ：Created in 2020-10-26
 * @Description：
 * @Modified By：
 * @Version:
 */
@Component
@Slf4j
public class ScheduleTaskService {
    @Resource
    private RedisService redisService;
    @Resource
    private ArticleVODao articleVODao;

    @Resource
    private MusicPoolMapper musicPoolMapper;
    @Scheduled(cron = "0 0/30 * ? * ?")
    private void updateArticlesGreatCount(){
        String greatPreKey = WebConst.REDIS_DATA_BASE + "." + WebConst.REDIS_ARTICLE_GREAT_USER_KEY + ".";
        System.out.println("update ");
        Set<Object> articlesIds = redisService.sMembers(WebConst.REDIS_DATA_BASE + "." + WebConst.REDIS_GREAT_ARTICLES_IDS);
        if (CollUtil.isEmpty(articlesIds)){
            return;
        }
        for (Object articleId : articlesIds) {
            Integer longId = (Integer) articleId;
            Long greatCount = redisService.sSize(greatPreKey + longId);
            log.debug("get id {} article new great count is {}", longId, greatCount);
            if (greatCount > 0){
                articleVODao.incrGreatCount(new Long(longId), greatCount);
                redisService.del(greatPreKey + longId);
            }
        }
        redisService.del(WebConst.REDIS_DATA_BASE + "." + WebConst.REDIS_GREAT_ARTICLES_IDS);
    }
    // 每月最后一天 23 ： 15
//    @Scheduled(cron="0 15 23 L * ?")
//    private void clearPoolsRecentUser(){
//
//    }
}
